/**
 * 
 */
package com.jm.web.form;


import java.io.IOException;

import javax.servlet.jsp.JspWriter;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.form.support.FormItem;
import com.jm.web.tags.BaseDPTag;

/**
 * @author yanjie
 *
 * 2011/9/19 下午12:35:39
 */
public class TabItem extends FormItem{
	protected DPanel form = null ;
	protected Iframe iframe = null;
	
	/**
	 * @param tag
	 */
	public TabItem() {}
	
	@Override
	public void outItemBegin(JspWriter writer) {
		BaseDPTag dpTag =  (BaseDPTag)this.getBaseTag().getParent();
		if(this.getBaseTag().GetExtValue("layout",true)){
			StringBuilderEx sb = new StringBuilderEx();
			sb.Append("\r\n<div id=\"%1$s\" ifrid=\"%2$s\" delay=\"%3$s\" style=\"height: auto;padding-top:2px;padding-right:0px;\">",
					this.getId(),
					this.iframe != null ? "ifr_"+this.getId() :"",
					this.getBaseTag().GetExtValue("delay", false) ? "true" :"",
					dpTag.isHiddeTab(this.GetExtValue("tabid", "null")) ? "none" : ""
			);
			try {
				writer.write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void outItemBody(JspWriter writer) {
		if(this.form != null)
			form.onRender(writer);
		else if(this.iframe != null){
			StringBuilderEx sb = new StringBuilderEx();
			sb.Append("<iframe src=\"%1$s\" style=\"padding:0px;\" ",this.iframe.getSrc());
			sb.Append(" scrolling=\"%1$s\"",this.iframe.GetExtValue("scrolling", "no"));
			sb.Append(" rsrc=\"%1$s\"",this.iframe.getRsrc());
			sb.Append(" id=\"ifr_%1$s\"",this.getId());			
			sb.Append(" width=\"%1$s\"",StringHelper.getValue(this.iframe.getWidth(),"100%"));
			sb.Append(" height=\"%1$s\"",StringHelper.getValue(this.iframe.getHeight(),"300px"));
			sb.Append(" frameBorder=\"0\"");
			sb.Append(" ></iframe>");
			try {
				writer.write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void outItemEnd(JspWriter writer) {
		if(this.getBaseTag().GetExtValue("layout",true)){
			StringBuilderEx sb = new StringBuilderEx();
			sb.Append("\r\n</div>");
			try {
				writer.write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public DPanel getForm() {
		return form;
	}
	
	public void setForm(DPanel form) {
		this.form = form;
	}

	public Iframe getIframe() {
		return iframe;
	}

	public void setIframe(Iframe iframe) {
		this.iframe = iframe;
	}

}
